package com.ruoyi.notice.service.impl;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.notice.domain.SysMaterial;
import com.ruoyi.notice.mapper.SysMaterialMapper;
import com.ruoyi.notice.service.SysMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author YL
 * @version 1.0
 * @date 2020/6/23 9:22
 */
@Service
public class SysMaterialServiceImpl implements SysMaterialService {
    @Autowired
    private SysMaterialMapper sysMaterialMapper;

    /**
     * 查询所有物料信息
     * @return
     */
    @Override
    public List<SysMaterial> selectAll(){
        return sysMaterialMapper.selectAll();
    }

    /**
     * 查询物料
     *
     * @param id 物料ID
     * @return 物料
     */
    @Override
    public SysMaterial selectSysMaterialById(Long id)
    {
        return sysMaterialMapper.selectSysMaterialById(id);
    }

    /**
     * 查询物料列表
     *
     * @param sysMaterial 物料
     * @return 物料
     */
    @Override
    public List<SysMaterial> selectSysMaterialList(SysMaterial sysMaterial)
    {
        return sysMaterialMapper.selectSysMaterialList(sysMaterial);
    }

    /**
     * 新增物料
     *
     * @param sysMaterial 物料
     * @return 结果
     */
    @Override
    public int insertSysMaterial(SysMaterial sysMaterial)
    {
        String now=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
        sysMaterial.setCreationTime(now);
        List<SysMaterial> sysMaterials=sysMaterialMapper.selectAll();
        boolean flag=true;
        for (SysMaterial material:sysMaterials){
            if (material.getMaterialNum().equals(sysMaterial.getMaterialNum())){
                flag=false;
                break;
            }
        }
        if (!flag){
            return -1;
        }
        return sysMaterialMapper.insertSysMaterial(sysMaterial);
    }

    /**
     * 修改物料
     *
     * @param sysMaterial 物料
     * @return 结果
     */
    @Override
    public int updateSysMaterial(SysMaterial sysMaterial)
    {
        return sysMaterialMapper.updateSysMaterial(sysMaterial);
    }

    /**
     * 批量删除物料
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteSysMaterialByIds(String ids)
    {
        return sysMaterialMapper.deleteSysMaterialByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除物料信息
     *
     * @param id 物料ID
     * @return 结果
     */
    @Override
    public int deleteSysMaterialById(Long id)
    {
        return sysMaterialMapper.deleteSysMaterialById(id);
    }
}
